package com.yyfax.ph.common.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.*;
import android.widget.*;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.yyfax.ph.R;

import java.util.List;

/**
 * 底部弹出的包含GridView的Dialog
 * 注意: Dialog布局从上到下只包含 标题Tv,GridView,取消按钮Btn;
 *       GridView item布局从上到下只包含 icon图标Iv, 项目说明Tv;
 * @author ying_he
 * @date 2017/4/6 17:42
 */
public class BottomGridDialog extends Dialog {
    public BottomGridDialog(Context context, int themeId) {
        super(context, themeId);
    }

    public interface BottomGridItemClickListener {
        void onBottomGridItemClick(AdapterView<?> parent, View view, int type, long id);
    }

    public static class Builder {
        @BindView(R.id.id_dialog_title_tv)
        TextView mTitleTv;
        @BindView(R.id.id_dialog_negative_btn)
        Button mNegativeBtn;
        @BindView(R.id.id_dialog_gv)
        GridView mGridView;

        int mDialogLayoutId;
        int mGridItemLayoutId;

        private Context mContext;
        private String mTitle;
        private String mNegativeBtnText;
        private OnClickListener mNegativeBtnListener;
        private List<ItemBean> mDataList;
        private BottomGridItemClickListener mBottomGridItemClickListener;

        /**
         * @param context          上下文
         * @param dialogLayoutId   dialog的布局
         * @param gridItemLayoutId GridView的item布局
         */
        public Builder(Context context, int dialogLayoutId, int gridItemLayoutId) {
            this.mContext = context;
            this.mDialogLayoutId = dialogLayoutId;
            this.mGridItemLayoutId = gridItemLayoutId;
        }

        public Builder setTitle(String title) {
            this.mTitle = title;
            return this;
        }

        public Builder setData(List<ItemBean> dataList) {
            this.mDataList = dataList;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.mNegativeBtnText = negativeButtonText;
            this.mNegativeBtnListener = listener;
            return this;
        }

        public Builder setOnItemClickListener(BottomGridItemClickListener bottomGridItemClickListener) {
            this.mBottomGridItemClickListener = bottomGridItemClickListener;
            return this;
        }

        /**
         * 手动设置子View 的id
         * 注意这里是依次循环遍历ViewGroup的子View,请确保ViewGroup的子View顺序一致.
         * @param viewGroup
         */
        private void setIds(ViewGroup viewGroup) {
            if (viewGroup != null) {
                for (int i = 0; i < viewGroup.getChildCount(); i++) {
                    View child = viewGroup.getChildAt(i);
                    if (child != null) {
                        switch (i) {
                            case 0:
                                child.setId(R.id.id_dialog_title_tv);
                                break;
                            case 1:
                                child.setId(R.id.id_dialog_gv);
                                break;
                            case 3:
                                child.setId(R.id.id_dialog_negative_btn);
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }

        public BottomGridDialog create() {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final BottomGridDialog dialog = new BottomGridDialog(mContext, R.style.PopBottomGridDialogTheme);
            ViewGroup layout = (ViewGroup) inflater.inflate(mDialogLayoutId, null);
            setIds(layout);
            ButterKnife.bind(this, layout);
            initWidget(dialog);
            config(dialog);
            dialog.setContentView(layout);
            GridViewAdapter adapter = new GridViewAdapter(mContext, mDataList, mGridItemLayoutId);
            mGridView.setAdapter(adapter);
            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    ItemBean gridItemBean = (ItemBean)mGridView.getAdapter().getItem(position);
                    mBottomGridItemClickListener.onBottomGridItemClick(parent, view, gridItemBean.itemType, id);
                }
            });
            mGridView.setOverScrollMode(View.OVER_SCROLL_NEVER);
            return dialog;
        }

        private void initWidget(final Dialog dialog) {
            if (!TextUtils.isEmpty(mTitle)) {
                mTitleTv.setText(mTitle);
            } else {
                mTitleTv.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(mNegativeBtnText)) {
                mNegativeBtn.setText(mNegativeBtnText);
                if (mNegativeBtnListener != null) {
                    mNegativeBtn.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            mNegativeBtnListener.onClick(dialog,
                                    DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            } else {
                mNegativeBtn.setVisibility(View.GONE);
            }
        }

        private void config(Dialog dialog) {
            Window dialogWindow = dialog.getWindow(); // 获取当前Activity所在的窗体
            dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
            dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
            dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

            WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获得窗体的属性
            lp.y = 0; // 设置Dialog距离底部的距离
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.BOTTOM;
            dialogWindow.setAttributes(lp);
        }
    }

    public static class GridViewAdapter extends BaseAdapter {
        int mGridItemLayoutId;
        private LayoutInflater mInflater;
        private List<ItemBean> mDataList;

        GridViewAdapter(Context context, List<ItemBean> dataList, int gridItemLayoutId) {
            mDataList = dataList;
            mGridItemLayoutId = gridItemLayoutId;
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return mDataList != null ? mDataList.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return mDataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        /**
         * 手动设置子View 的id
         * 注意这里是依次循环遍历ViewGroup的子View,请确保ViewGroup的子View顺序一致.
         * @param viewGroup
         */
        private void setIds(ViewGroup viewGroup) {
            if (viewGroup != null) {
                for (int i = 0; i < viewGroup.getChildCount(); i++) {
                    View child = viewGroup.getChildAt(i);
                    if (child != null) {
                        switch (i) {
                            case 0:
                                child.setId(R.id.id_icon_item_iv);
                                break;
                            case 1:
                                child.setId(R.id.id_des_item_tv);
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(mGridItemLayoutId, parent, false);
                setIds((ViewGroup)convertView);
                holder = new ViewHolder(convertView);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.bindView(position);
            return convertView;
        }

        public class ViewHolder {
            @BindView(R.id.id_icon_item_iv)
            ImageView itemIconIv;
            @BindView(R.id.id_des_item_tv)
            TextView itemDesTv;

            public ViewHolder(View view) {
                ButterKnife.bind(this, view);
                view.setTag(this);
            }

            void bindView(int position) {
                itemIconIv.setImageResource(mDataList.get(position).itemIconId);
                itemDesTv.setText(mDataList.get(position).itemDes);
            }
        }
    }

    public static class ItemBean {
        /** 类型,用于点击类型 */
        public int itemType;
        int itemDes;
        int itemIconId;

        public ItemBean(int itemType,int itemDes, int itemIconId) {
            this.itemType = itemType;
            this.itemDes = itemDes;
            this.itemIconId = itemIconId;
        }
    }
}
